﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PhotoGallery.Models;
using PhotoGallery.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PhotoGallery.Utils
{
    public static class DatabaseExtensions
    {
        public static IWebHost InitPhotoGalleryRepo(this IWebHost webHost, string[] args)
        {
            try
            {
                using (var scope = webHost.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;
                    var dbContext = services.GetRequiredService<PhotoGalleryRepository>();
                    var config = services.GetRequiredService<IConfiguration>();
                    var env = services.GetRequiredService<IHostingEnvironment>();
                    var rootPath = env.WebRootPath;
                    int app_id = 0;
                    int.TryParse(config["CosCloud:APP_ID"], out app_id);
                    Utils.ServerHelper.APP_ID = app_id;
                    Utils.ServerHelper.SECRET_ID = config["CosCloud:SECRET_ID"];
                    Utils.ServerHelper.SECRET_KEY = config["CosCloud:SECRET_KEY"];
                    Utils.ServerHelper.BucketName = config["CosCloud:BucketName"];
                    Utils.ServerHelper.RemotePath = config["CosCloud:RemotePath"];
                    var builder = new ConfigurationBuilder();
                    var cmd = builder.AddCommandLine(args).Build();
                    //首次运行，执行搜索网站根目录下albums目录里的图片文件
                    if ("y".Equals(cmd["FirstRun"],StringComparison.OrdinalIgnoreCase)) 
                    {
                        Utils.ServerHelper.InitializeAlbumsCollection(dbContext, rootPath);
                        AddDefaultUser(dbContext);
                    }

                }
            }
            catch (Exception)
            {
                throw;
            }


            return webHost;
        }

        /// <summary>
        /// 添加默认用户
        /// </summary>
        /// <param name="repo"></param>
        private static void AddDefaultUser(PhotoGalleryRepository repo)
        {

            var newUser = new User
            {
                CreateDate = DateTime.Now,
                IsEnable = true,
                Name = "ranran",
                Salt = "3OsqdkqFBzsX",
                PasswdHash = "9F2C6FABE4FA19090BFD5FA629A35E8BC7359D6EBA1DF8664740798C2812F550",
                UserName = "baby"
            };
            var user = repo.Users.IgnoreQueryFilters().FirstOrDefault(w => w.UserName == newUser.UserName);
            if (user == null)
            {
                repo.Users.Add(newUser);
                repo.SaveChanges();
            }

            
            var newUser2 = new User
            {
                CreateDate = DateTime.Now,
                IsEnable = true,
                Name = "聪",
                Salt = "AAdpK@%Cx@",
                PasswdHash = "DA2D490BFD3F38C7424FA84E0121827B5FAE096BABFCC3D577D8F28B75EAE28C",
                UserName = "murry"
            };

            user = repo.Users.IgnoreQueryFilters().FirstOrDefault(w => w.UserName == newUser2.UserName);
            if (user == null)
            {
                repo.Users.Add(newUser2);
                repo.SaveChanges();
            }

        }


    }
}
